package com.jjh.interceptor;

import com.jjh.annation.AutoId;
import com.jjh.annation.CreateTime;
import com.jjh.annation.UpdateTime;
import com.jjh.utils.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})})
public class MybatisAutoFillingInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        // 获取 SQL 命令
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        //只判断新增和修改
        if (SqlCommandType.INSERT.equals(sqlCommandType) || SqlCommandType.UPDATE.equals(sqlCommandType)) {
            // 获取参数
            Object parameter = invocation.getArgs()[1];
            //批量操作时
//            if (parameter instanceof MapperMethod.ParamMap) {
//                MapperMethod.ParamMap map = (MapperMethod.ParamMap) parameter;
//                Object obj = map.get("list");
//                List<?> list = (List<?>) obj;
//                if (list != null) {
//                    for (Object o : list) {
//                        setParameter(o, sqlCommandType);
//                    }
//                }
//            } else {
                setParameter(parameter, sqlCommandType);
            //}
        }
        return invocation.proceed();
    }

    public void setParameter(Object parameter, SqlCommandType sqlCommandType) throws Throwable {
        Class<?> aClass = parameter.getClass();
        Field[] declaredFields;
        declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            LocalDateTime now = LocalDateTime.now();
            if (SqlCommandType.INSERT.equals(sqlCommandType)) {
                if (field.getAnnotation(CreateTime.class) != null) {
                    field.setAccessible(true);
                    if (field.get(parameter) == null) {
                        field.set(parameter, now);
                    }
                }
                if (field.getAnnotation(UpdateTime.class) != null) {
                    field.setAccessible(true);
                    if (field.get(parameter) == null) {
                        field.set(parameter, now);
                    }
                }
                if (field.getAnnotation(AutoId.class) != null ) {
                    field.setAccessible(true);
                    if (field.get(parameter) == null) {
                        if (Objects.equals(field.getAnnotation(AutoId.class).type(), AutoId.IdType.SNOWFLAKE)) {
                            field.set(parameter, IdUtil.getSnowflakeNextId());
                        } else if (Objects.equals(field.getAnnotation(AutoId.class).type(), AutoId.IdType.UUID)) {
                            field.set(parameter, IdUtil.simpleUUID());
                        }
                    }
                }
            }
            if (SqlCommandType.UPDATE.equals(sqlCommandType)) {
                if (field.getAnnotation(UpdateTime.class) != null) {
                    field.setAccessible(true);
                    if (field.get(parameter) == null) {
                        field.set(parameter, now);
                    }
                }
            }
        }
    }
}
